package com.tx.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tx.entity.FileInfo;
import com.tx.entity.Page;
import com.tx.entity.Type;
import com.tx.service.TypeService;
import com.tx.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: tcx
 * @description: 博客类型控制器
 * @Date: 2019/11/5
 **/
@Controller("typeController")
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private TypeService typeService;

    /* *
     * @description:  打开列表
     * @author tcx
     * @date 2019/11/5
     * @param map
     * @return java.lang.String
     */
    @RequestMapping("/teacher/list")
    public String list(Map<String,Object> map, HttpSession session){
        /*//查询出博客类型列表
        List<Type> list = typeService.getAll();
        //存入session中
        session.setAttribute("types",list);
        map.put("list",list);*/
        return "teacher_btypelist";
    }
    /* *
     * @description:  打开修改界面
     * @author tcx
     * @date 2019/11/5
     * @param tId
     * @param map
     * @return java.lang.String
     */
    @RequestMapping(value = "/teacher/to_update",params = "tId")
    public String edit(Integer tId,Map<String,Object> map){
        Type type = new Type();
        type.settState(1);
        map.put("list",typeService.getAll(type));
        map.put("type",typeService.get(tId));
        return "teacher_btypeedit";
    }
    /* *
     * @description:  修改
     * @author tcx
     * @date 2019/11/5
     * @param type
     * @return java.lang.String
     */
    @RequestMapping("/teacher/update")
    public String update(Type type,HttpSession session){
        typeService.edit(type);
        session.removeAttribute("types");
        type.settState(1);
        session.setAttribute("types",typeService.getAll(type));
        return "redirect:/type/teacher/list.do";
    }
    @ResponseBody
    @RequestMapping("/teacher/editState")
    public String updatState(Type type,HttpSession session){
        int resultTotal = typeService.edit(type);
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        if (resultTotal > 0) {
            map.put("code",0);
            map.put("msg","修改成功");
            session.removeAttribute("types");
            type.settState(1);
            session.setAttribute("types",typeService.getAll(type));
        } else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }

        return gson.toJson(map);
    }
/* *
 * @description:  删除博客类型
 * @author tcx
 * @date 2019/11/22
 * @param request
 * @return java.lang.String
 */
    @ResponseBody
    @RequestMapping("/teacher/remove")
    public String remove(HttpServletRequest request,HttpSession session){
        Integer tId = Integer.parseInt(request.getParameter("tId"));
        int resultTotal = typeService.remove(tId);
        Map<String, Object> map = new HashMap<String, Object>();
        if (resultTotal > 0) {
            map.put("code", 200);
            map.put("msg", "删除成功");
            session.removeAttribute("types");
            Type type = new Type();
            type.settState(1);
            session.setAttribute("types",typeService.getAll(type));
        }else{
            map.put("code", -1);
            map.put("msg", "删除失败");
        }
        Gson gson = new Gson();
        return  gson.toJson(map);
    }
    /* *
     * @description:  添加分类
     * @author tcx
     * @date 2019/11/5
     * @param type
     * @return java.lang.String
     */
    @RequestMapping("/teacher/add")
    public String add(Type type,HttpSession session){
        typeService.add(type);
        session.removeAttribute("types");
        type.settState(1);
        session.setAttribute("types",typeService.getAll(type));
        return "redirect:/type/teacher/list.do";
    }
    /*======================================admin控制==================================*/

    @RequestMapping("/teacher/admin_list")
    @ResponseBody
    public String list(@RequestParam(value = "page", required = false) Integer currentPage,
                       @RequestParam(value = "limit", required = false) Integer rows,
                       @RequestParam(value = "keywords", required = false) String keywords){
        Page pageBean = new Page();
        pageBean.setCurrentPage(currentPage);//设置当前页
        pageBean.setEveryPage(rows);//设置每页显示数量
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", pageBean.getStart());//设置起始页
        map.put("size", pageBean.getEveryPage());//设置页面大小
        map.put("q", StringUtil.formatLike(keywords));
        List<Type> list = typeService.list(map);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm").create();
        Integer total = typeService.getTotal(map);//获得总数据
        map.clear();
        if(list!=null){
            map.put("data", list);
            map.put("count", total);
            map.put("code", 0);
            map.put("msg", "");
        }else{
            map.put("data", list);
            map.put("count", total);
            map.put("code", 1);
            map.put("msg", "");
        }
        return  gson.toJson(map);
    }
    /* *
     * @description:  删除分类
     * @author tcx
     * @date 2020/04/05
     * @param request
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/teacher/admin_delete")
    public String delete(HttpServletRequest request){
        String[] idsStr = request.getParameter("ids").split(",");
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < idsStr.length; i++) {
            typeService.remove(Integer.parseInt(idsStr[i]));
        }
        map.put("msg", 200);
        return  gson.toJson(map);
    }
    //分类禁用
    @ResponseBody
    @RequestMapping("/type/state.do")
    public String adminupdate(Type type,HttpSession session){
        int resultTotal = typeService.state(type);
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        if (resultTotal > 0) {
            map.put("code",0);
            map.put("msg","修改成功");
            session.removeAttribute("types");
            type.settState(1);
            session.setAttribute("types",typeService.getAll(type));
        } else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }

        return gson.toJson(map);
    }

    /* *
     * @description:修改类型名
     * @author tcx
     * @date 2020/5/13
     * @param fileInfo
     * @param session
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/teacher/admin_update")
    public String adminupdateName(Type type, HttpSession session){
        int resultTotal = typeService.edit(type);
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        if (resultTotal > 0) {
            map.put("code",0);
            map.put("msg","修改成功");
        } else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }

        return gson.toJson(map);
    }

}
